package com.smarttiger.zero.admin.util;

import com.smarttiger.zero.admin.entity.*;
import com.smarttiger.zero.admin.service.*;
import com.smarttiger.zero.admin.entity.*;
import com.smarttiger.zero.admin.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

import java.util.*;

@Configuration
public class InitDataConfig implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    MenuService menuService;

    @Autowired
    UsersService usersService;

    @Autowired
    DictionaryService dictionaryService;

    @Autowired
    DictionaryDetailService dictionaryDetailService;

    @Autowired
    UsersGroupService usersGroupService;

    @Autowired
    InterfaceService interfaceService;

    @Autowired
    LogicService logicService;

    @Autowired
    ArgsService argsService;

    @Autowired
    RoleMenuService roleMenuService;

    @Autowired
    DataSourceService dataSourceService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {

        DataSourceModel ds = new DataSourceModel();
        ds.setCode("H2");
        ds.setName("MAIN");
        ds.setUserName("sa");
        ds.setPass("sa");
        ds.setUrl("jdbc:h2:mem:h2test");
        dataSourceService.save(ds);

        MenuModel root = new MenuModel();
        root.setName("ZERO");
        root.setIcon("");
        root.setPath("");
        List<MenuModel> rootlist = new ArrayList<>();

        List<String> menuIds = new ArrayList<>();
        List<String> menuIdsone = new ArrayList<>();
        //菜单配置
        MenuModel m = new MenuModel();
        m.setName("系统配置");
        m.setIcon("el-icon-zero-xitongpeizhi");
        m.setPath("");

        MenuModel m2 = new MenuModel();
        m2.setName("字典");
        m2.setIcon("el-icon-zero-zidian");
        m2.setPath("/dic");

        MenuModel m3 = new MenuModel();
        m3.setName("数据源");
        m3.setIcon("el-icon-zero-shujuyuan");
        m3.setPath("/datasource");

        List<MenuModel> list = new ArrayList<>();
        list.add(m2);
        menuIds.add(m2.getId());
        list.add(m3);
        menuIds.add(m3.getId());
        m.setMenuModelList(list);
        rootlist.add(m);


        m = new MenuModel();
        m.setName("用户管理");
        m.setIcon("el-icon-zero-yonghu3");
        m.setPath("");
        m2 = new MenuModel();
        m2.setName("用户");
        m2.setIcon("el-icon-zero-yonghu");
        m2.setPath("/user");
        menuIds.add(m2.getId());
        m3 = new MenuModel();
        m3.setName("用户组");
        m3.setIcon("el-icon-zero-user-group");
        m3.setPath("/userGroup");
        menuIds.add(m3.getId());
        list = new ArrayList<>();
        list.add(m2);
        list.add(m3);
        m.setMenuModelList(list);
        rootlist.add(m);


        m = new MenuModel();
        m.setName("消息管理");
        m.setIcon("el-icon-zero-xiaoxi");
        m.setPath("");
        m2 = new MenuModel();
        m2.setName("消息列表");
        m2.setIcon("el-icon-zero-xiaoxiduilie");
        m2.setPath("/messageList");
        menuIds.add(m2.getId());
        m3 = new MenuModel();
        m3.setName("消息订阅");
        m3.setIcon("el-icon-zero-mqxiaoxiduilieMQ");
        m3.setPath("/messageScript");
        menuIds.add(m3.getId());
        list = new ArrayList<>();
        list.add(m2);
        list.add(m3);
        m.setMenuModelList(list);
        rootlist.add(m);

        m = new MenuModel();
        m.setName("任务管理");
        m.setIcon("el-icon-zero-renwuzhongxin");
        m.setPath("");
        m2 = new MenuModel();
        m2.setName("任务列表");
        m2.setIcon("el-icon-zero-renwu");
        m2.setPath("/task");
        menuIds.add(m2.getId());
        list = new ArrayList<>();
        list.add(m2);
        m.setMenuModelList(list);
        rootlist.add(m);

        m = new MenuModel();
        m.setName("逻辑块管理");
        m.setIcon("el-icon-zero-wangluojiekou");
        m.setPath("");
        m2 = new MenuModel();
        m2.setName("逻辑块");
        m2.setIcon("el-icon-zero-shitichuliluoji");
        m2.setPath("/logic");
        menuIds.add(m2.getId());
        list = new ArrayList<>();
        list.add(m2);
        m.setMenuModelList(list);
        rootlist.add(m);

        m = new MenuModel();
        m.setName("接口管理");
        m.setIcon("el-icon-zero-fuwujiekou");
        m.setPath("");
        m2 = new MenuModel();
        m2.setName("接口配置");
        m2.setIcon("el-icon-zero-jiekou1");
        m2.setPath("/interfaceConfig");
        menuIds.add(m2.getId());

        m3 = new MenuModel();
        m3.setName("接口示例");
        m3.setIcon("el-icon-zero-jiekou");
        m3.setPath("/interfaceInfo");

        menuIds.add(m3.getId());
        menuIdsone.add(m3.getId());
        list = new ArrayList<>();
        list.add(m2);
        list.add(m3);
        m.setMenuModelList(list);
        rootlist.add(m);

        m = new MenuModel();
        m.setName("日志管理");
        m.setIcon("el-icon-zero-rizhi3");
        m.setPath("");
        m2 = new MenuModel();
        m2.setName("日志列表");
        m2.setIcon("el-icon-zero-rizhi2");
        m2.setPath("/logs");
        menuIds.add(m2.getId());
        list = new ArrayList<>();
        list.add(m2);
        m.setMenuModelList(list);
        rootlist.add(m);

        m = new MenuModel();
        m.setName("系统监控");
        m.setIcon("el-icon-zero-jiankong2");
        m.setPath("");
        m2 = new MenuModel();
        m2.setName("监控表盘");
        m2.setIcon("el-icon-zero-jiankong4");
        m2.setPath("/monitoring");
        menuIds.add(m2.getId());
        list = new ArrayList<>();
        list.add(m2);
        m.setMenuModelList(list);
        rootlist.add(m);


        m = new MenuModel();
        m.setName("权限管理");
        m.setIcon("el-icon-zero-quanxianguanli1");
        m.setPath("");

        m2 = new MenuModel();
        m2.setName("菜单配置");
        m2.setIcon("el-icon-zero-shitichuliluoji");
        m2.setPath("/menu");
        menuIds.add(m2.getId());
        m3 = new MenuModel();
        m3.setName("菜单权限配置");
        m3.setIcon("el-icon-zero-quanxian");
        m3.setPath("/menuConfig");
        menuIds.add(m3.getId());
        list = new ArrayList<>();
        list.add(m2);
        list.add(m3);
        m.setMenuModelList(list);
        rootlist.add(m);
        root.setMenuModelList(rootlist);
        menuService.save(root);


        //用户
        UsersModel u = new UsersModel();
        u.setName("系统管理员");
        u.setUserName("admin");
        u.setPassword("123456");
        u.setToken("dddd");
        usersService.save(u);

        List<UsersModel> ul = new ArrayList<>();
        ul.add(u);
        UsersGroupModel ug = new UsersGroupModel();
        ug.setNote("系统管理员，有全部权限。");
        ug.setName("管理员组");
        ug.setCode("GLYZ");
        ug.setUsersModelList(ul);
        usersGroupService.save(ug);

        Map<String,Object> rms = new HashMap<>();
        rms.put("groupId",ug.getId());
        rms.put("ids",menuIds);
        roleMenuService.saveRoleMenus(rms);

        ug = new UsersGroupModel();
        ug.setNote("只有接口示例查看权限");
        ug.setName("接口访问组");
        ug.setCode("JKZ");
        usersGroupService.save(ug);

        rms = new HashMap<>();
        rms.put("groupId",ug.getId());
        rms.put("ids",menuIdsone);
        roleMenuService.saveRoleMenus(rms);


        //基础logic
        String uuid = UUID.randomUUID().toString();
        LogicModel lm = new LogicModel();
        lm.setId(uuid);
        lm.setName("SQL逻辑块");
        lm.setClassName("com.smarttiger.zero.logic.impl.SqlLogic");
        lm.setNote("数据库查询逻辑块，参数sql 值sql语句，参数用?代替。其他参数设默认值，直接按顺序拼入sql,如果默认值为空，从接口读取。");
        lm.setDataSourceId(ds.getId());
        logicService.save(lm);

        ArgsModel argsModel = new ArgsModel();
        argsModel.setLogicId(uuid);
        argsModel.setName("sql");
        argsModel.setTypecode("java.lang.String");
        argsModel.setDefaultValue(" select * from USERS ");
        argsService.save(argsModel);


        //基础logic
        uuid = UUID.randomUUID().toString();
        lm = new LogicModel();
        lm.setId(uuid);
        lm.setName("WS逻辑块");
        lm.setClassName("com.smarttiger.zero.logic.impl.WebServiceLogic");
        lm.setNote("wsdl 地址, method 方法, param 参数");
        logicService.save(lm);

        argsModel = new ArgsModel();
        argsModel.setLogicId(uuid);
        argsModel.setName("wsdl");
        argsModel.setTypecode("java.lang.String");
        argsModel.setDefaultValue("");
        argsService.save(argsModel);

        argsModel = new ArgsModel();
        argsModel.setLogicId(uuid);
        argsModel.setName("method");
        argsModel.setTypecode("java.lang.String");
        argsModel.setDefaultValue("");
        argsService.save(argsModel);

        argsModel = new ArgsModel();
        argsModel.setLogicId(uuid);
        argsModel.setName("param");
        argsModel.setTypecode("java.lang.String");
        argsModel.setDefaultValue("");
        argsService.save(argsModel);

        //字典配置
        //参数类型
        DictionaryModel dictionaryModel = new DictionaryModel();
        dictionaryModel.setCode("ArgsType");
        dictionaryModel.setLabel("参数类型");
        dictionaryService.save(dictionaryModel);

        DictionaryDetailModel  subDic= new DictionaryDetailModel();
        subDic.setKey("java.lang.String");
        subDic.setLabel("String");
        subDic.setParentCode("ArgsType");
        dictionaryDetailService.save(subDic);

        subDic= new DictionaryDetailModel();
        subDic.setKey("java.math.BigDecimal");
        subDic.setLabel("BigDecimal");
        subDic.setParentCode("ArgsType");
        dictionaryDetailService.save(subDic);

        subDic= new DictionaryDetailModel();
        subDic.setKey("java.util.Date");
        subDic.setLabel("Date");
        subDic.setParentCode("ArgsType");
        dictionaryDetailService.save(subDic);

        subDic= new DictionaryDetailModel();
        subDic.setKey("java.lang.Integer");
        subDic.setLabel("Integer");
        subDic.setParentCode("ArgsType");
        dictionaryDetailService.save(subDic);

        subDic= new DictionaryDetailModel();
        subDic.setKey("java.lang.Double");
        subDic.setLabel("Double");
        subDic.setParentCode("ArgsType");
        dictionaryDetailService.save(subDic);

        //是否
        dictionaryModel = new DictionaryModel();
        dictionaryModel.setCode("YN");
        dictionaryModel.setLabel("是否");
        dictionaryService.save(dictionaryModel);

        subDic= new DictionaryDetailModel();
        subDic.setKey("Y");
        subDic.setLabel("是");
        subDic.setParentCode("YN");
        dictionaryDetailService.save(subDic);

        subDic= new DictionaryDetailModel();
        subDic.setKey("N");
        subDic.setLabel("否");
        subDic.setParentCode("YN");
        dictionaryDetailService.save(subDic);


        //返回类型
        dictionaryModel = new DictionaryModel();
        dictionaryModel.setCode("ReturnType");
        dictionaryModel.setLabel("接口返回数据类型");
        dictionaryService.save(dictionaryModel);

        subDic= new DictionaryDetailModel();
        subDic.setKey("json");
        subDic.setLabel("JSON");
        subDic.setParentCode("ReturnType");
        dictionaryDetailService.save(subDic);

        subDic= new DictionaryDetailModel();
        subDic.setKey("xml");
        subDic.setLabel("XML");
        subDic.setParentCode("ReturnType");
        dictionaryDetailService.save(subDic);

        dictionaryModel = new DictionaryModel();
        dictionaryModel.setCode("SYS_CODE");
        dictionaryModel.setLabel("系统编号");
        dictionaryService.save(dictionaryModel);

        subDic= new DictionaryDetailModel();
        subDic.setKey("XS");
        subDic.setLabel("销售系统");
        subDic.setParentCode("SYS_CODE");
        dictionaryDetailService.save(subDic);

        dictionaryModel = new DictionaryModel();
        dictionaryModel.setCode("D_DRIVER");
        dictionaryModel.setLabel("数据源驱动");
        dictionaryService.save(dictionaryModel);

        subDic= new DictionaryDetailModel();
        subDic.setKey("org.h2.Driver");
        subDic.setLabel("H2驱动");
        subDic.setParentCode("D_DRIVER");
        dictionaryDetailService.save(subDic);



        dictionaryModel = new DictionaryModel();
        dictionaryModel.setCode("SUB_TYPE");
        dictionaryModel.setLabel("订阅类型");
        dictionaryService.save(dictionaryModel);

        subDic= new DictionaryDetailModel();
        subDic.setKey("ws");
        subDic.setLabel("WebService");
        subDic.setParentCode("SUB_TYPE");
        dictionaryDetailService.save(subDic);

        subDic= new DictionaryDetailModel();
        subDic.setKey("http");
        subDic.setLabel("Http");
        subDic.setParentCode("SUB_TYPE");
        dictionaryDetailService.save(subDic);

        dictionaryModel = new DictionaryModel();
        dictionaryModel.setCode("CRON");
        dictionaryModel.setLabel("调用频率");
        dictionaryService.save(dictionaryModel);

        subDic= new DictionaryDetailModel();
        subDic.setKey("0 0/1 * * * ? ");
        subDic.setLabel("每1分钟");
        subDic.setParentCode("CRON");
        dictionaryDetailService.save(subDic);





    }
}
